package com.woshua.cameramodel;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class CameraView extends SurfaceView {

    private SurfaceHolder holder = null;

    private Camera mCamera;


    public CameraView(Context context) {
        super(context);

        // 操作surface的holder
        holder = this.getHolder();

        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                //当预览视图创建的时候开启相机
                mCamera = Camera.open();
                try {
                    //设置预览
                    mCamera.setPreviewDisplay(holder);
                } catch (IOException e) {
                    // 释放相机资源并置空
                    mCamera.release();
                    mCamera = null;
                }
            }

            //当surface视图数据发生变化时，处理预览信息
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                try {
                    int PreviewWidth = 0;
                    int PreviewHeight = 0;
                    Camera.Parameters parameters = mCamera.getParameters();
                    // 选择合适的预览尺寸
                    List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();

                    // 如果sizeList只有一个我们也没有必要做什么了，因为就他一个别无选择
                    if (sizeList.size() > 1) {
                        Iterator<Camera.Size> itor = sizeList.iterator();
                        while (itor.hasNext()) {
                            Camera.Size cur = itor.next();
                            if (cur.width >= PreviewWidth
                                    && cur.height >= PreviewHeight) {
                                PreviewWidth = cur.width;
                                PreviewHeight = cur.height;
                                break;
                            }
                        }
                    }
                    parameters.setPreviewSize(PreviewWidth, PreviewHeight); //获得摄像区域的大小
                    parameters.setPictureFormat(PixelFormat.JPEG);//设置照片输出的格式
                    parameters.set("jpeg-quality", 100);//设置照片质量
                    parameters.setPictureSize(PreviewWidth, PreviewHeight);//设置拍出来的屏幕大小
                    setCameraDisplayOrientation(270);
                    parameters.set("orientation", "portrait");
                    mCamera.setParameters(parameters);//把上面的设置 赋给摄像头

                    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                    mCamera.setPreviewDisplay(getHolder());//把摄像头获得画面显示在SurfaceView控件里面
                    mCamera.startPreview();//开始预览
                } catch (IOException e) {
                    Log.e("TAG", e.toString());
                }
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // 停止预览
                mCamera.stopPreview();
                // 释放相机资源并置空
                mCamera.release();
                mCamera = null;
            }
        });

        // 设置Push缓冲类型，说明surface数据由其他来源提供，而不是用自己的Canvas来绘图，在这里是由摄像头来提供数据
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    /*
     * 设置屏幕方向
     * */
    public void setCameraDisplayOrientation(int degrees) {
        if (null == mCamera)
            return;
        android.hardware.Camera.CameraInfo info =
                new android.hardware.Camera.CameraInfo();

        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360;  // compensate the mirror
        } else {  // back-facing
            result = (info.orientation - degrees + 360) % 360;
        }

        mCamera.setDisplayOrientation(result);
    }


    public Camera getCamera() {
        return mCamera;
    }


    public void changeFlashLight() {
        Camera.Parameters parameters = getCamera().getParameters();
        if (Camera.Parameters.FLASH_MODE_OFF.equals(parameters.getFlashMode())) {
            turnOn(parameters);
        }
        else
        {
            turnOff(parameters);
        }
    }

    /*
     * 打开摄像头
     * */
    private void turnOn(Camera.Parameters parameters) {
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        getCamera().setParameters(parameters);
    }

    /*
     * 关闭摄像头
     * */
    private void turnOff(Camera.Parameters parameters) {
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        getCamera().setParameters(parameters);
    }
}
